// 获取应用实例
const app = getApp()

Page({

  data: {
    modelid: '',
    op: '',
    wW: 200,
    wH: 200,
    model: {
      Comments: [], // 维修记录
      StockOuts: [], // 领料记录
      AvailableTeams: [], // 班组
      AvailableUsers: [], // 成员
    },
    allPlaces: [],
    places: [],
    categories: [],
    hasCamera: false,
    showVideo: false,
    videoUrl: '',
    showPop: false, // 是否显示蒙版(补充说明)
    showPopWG: false,
    type: 1,
    popTitle: '进度汇报',
    showPopZP: false, // 显示指派
    showPopSL: false, // 显示受理
    showPopFP: false, // 显示分配
    maxFiles: 3,
    files: [],
    step: 0,
    steps: [{
        id: 2,
        text: '待确认',
        desc: '',
      },
      {
        id: 3,
        text: '待指派',
        desc: '',
      },
      {
        id: 4,
        text: '待分配',
        desc: '',
      },
      {
        id: 8,
        text: '待维修',
        desc: '',
      },
      {
        id: 9,
        text: '待评价',
        desc: '',
      },
      {
        id: 10,
        text: '已完工',
        desc: '',
      },
    ],
    showDepart: false,
    showTeam: false,
    showPerson: false,
    remark: '已维修',
    star: 100,
    searchPlaceValue: '',
    showPlaceAction: false,
    filter: {
      placeId: 0,
      categoryId: 0,
      name: '',
      pp: '',
      gg: '',
      location: '',
    },
    categoryActiveIndex: 0,
    categoryActiveId: null,
    showCategory: false,
    showPlace: false,
    showPopZT: false,
    items: [],
    pageIndex: 0, // 当前页Index
    totalPages: 1, // 明细页数
    details: [], // 维修明细
    isExternal: false,
    responsibleDepartmentId: 0, // 维修部门
    responsibleTeamId: 0, // 维修班组
    departs: [],
    teams: [],
    persons: [],
    teamIndex: 0,
    teamName: '',
    departIndex: 0,
    departName: '',
    maintenancePersons: [], // 维修人员
    maintenancePersonNames: '',
  },
  onLoad(options) {
    let _this = this;
    let _id = options.id || '';
    let _op = options.op || '';
    const {
      windowHeight,
      windowWidth
    } = wx.getWindowInfo();
    _this.loadDetail(_id);
    _this.setData({
      op: _op,
      modelid: _id,
      wW: windowWidth,
      wH: windowHeight
    })
    _this.videoContext = wx.createVideoContext('myVideo'); //视频播放
  },
  tapImg(e) {
    let _this = this;
    let _idx = e.currentTarget.dataset.idx
    wx.previewImage({
      urls: _this.data.model.Pictures,
      current: _this.data.model.Pictures[_idx]
    });
  },
  tapImgMin(e) {
    let _this = this;
    const {
      idx,
      pics
    } = e.currentTarget.dataset;
    wx.previewImage({
      urls: pics,
      current: pics[idx]
    });
  },
  tapDelImg(e) {
    let _this = this;
    let _idx = e.currentTarget.dataset.idx
    let _files = _this.data.files;
    _files.splice(_idx, 1);
    _this.setData({
      files: _files
    });
  },
  addImage(e) {
    let _this = this;
    wx.chooseMedia({
      mediaType: ['image'],
      sourceType: ['camera'],
      sizeType: ['compressed'],
      success: function (res) {
        let _files = _this.data.files;
        if (res.tempFiles.length + _files.length > _this.data.maxFiles) {
          wx.showToast({
            icon: 'none',
            title: `最多允许上传${_this.data.maxFiles}张图片`
          });
          return;
        }
        let startIdx = _files.length;
        for (let i = 0; i < res.tempFiles.length; i++) {
          _files.push({
            img: res.tempFiles[i].tempFilePath,
            percent: 0,
            id: 0,
            unique: startIdx + i
          });
        }
        _this.setData({
          files: _files
        }, function () {
          for (let j = 0; j < res.tempFiles.length; j++) {
            _this.uploadImg(res.tempFiles[j].tempFilePath, startIdx + j);
          }
        });
      }
    });
  },
  uploadImg(file, idx) {
    let _this = this;
    var task = wx.uploadFile({
      url: `${app.apiRoot}/Picture/AsyncUpload`,
      filePath: file,
      name: 'qqfile',
      success(resp) {
        var data = JSON.parse(resp.data) //返回的是[字符串]，需要自己转为JSON格式
        let _files = _this.data.files;
        _files[idx].id = data.pictureId;
        _this.setData({
          files: _files
        });
      },
      fail(resp) {
        console.log('fail', resp);
      }
    });

    task.onProgressUpdate((res) => {
      let _files = _this.data.files;
      _files[idx].percent = res.progress;
      _this.setData({
        files: _files
      });
    });
  },
  loadDetail(id) {
    let _this = this;
    const {
      steps,
      filter,
    } = _this.data;
    app.ajax({
      url: 'Maintenance/View',
      data: {
        maintenanceId: id
      },
      again: false, // 重复调用
      success(resp) {
        if (resp.model) {
          const _departs = (resp.model.AvailableDeparts || []).map(s => ({
            id: s.Id,
            name: s.Text
          }));
          const _teams = (resp.model.AvailableTeams || []).map(s => ({
            id: s.Id,
            name: s.Text
          }));
          const _persons = (resp.model.AvailableUsers || []).map(s => ({
            Id: s.Id,
            Text: s.Text,
            Checked: false,
          }));
          const _allPlaces = resp.model.AvailablePlaces || [];
          const _places = _allPlaces;
          const _categories = (resp.model.AvailableCategories.filter(m => m.PId == 0) || []).map(s => {
            var children = (resp.model.AvailableCategories.filter(m => m.PId == s.Id) || []).map(d => {
              return {
                id: d.Id,
                text: d.Text
              };
            });
            children.unshift({
              id: s.Id,
              text: s.Text,
            });
            return {
              id: s.Id,
              text: s.Text,
              children
            };
          });
          let step = 0;
          step = steps.findIndex(m => m.id === resp.model.State);
          filter.placeId = resp.model.PlaceId;
          filter.PlaceName = resp.model.PlaceName;
          filter.categoryId = resp.model.CategoryId;
          filter.CategoryName = resp.model.CategoryName;
          filter.name = resp.model.Name;
          filter.pp = resp.model.Manufacturer;
          filter.gg = resp.model.Specification;
          filter.location = resp.model.Location;
          _this.setData({
            departs: _departs,
            teams: _teams,
            persons: _persons,
            places: _places,
            categories: _categories,
            allPlaces: _allPlaces,
            modelid: resp.model.Id,
            model: resp.model,
            step,
            filter
          });
        }
      }
    });
  },
  onShareAppMessage() {
    return {
      title: app.app.title,
      desc: app.app.desc,
      path: '/pages/repair/detail?id=' + this.data.modelid
    };
  },
  showPopJD() {
    let _this = this;
    _this.setData({
      type: 1,
      popTitle: '进度汇报',
      showPop: true
    });
  },
  showPopWG() {
    let _this = this;
    _this.setData({
      type: 2,
      popTitle: '完工汇报',
      showPopWG: true
    });
  },
  showPopPJ() {
    let _this = this;
    _this.setData({
      type: 9,
      popTitle: '维修效果评价',
      showPop: true
    });
  },
  showPopZP() {
    let _this = this;
    _this.setData({
      showPopZP: true
    });
  },
  showPopSL() {
    let _this = this;
    _this.setData({
      showPopSL: true
    });
  },
  showPopFP() {
    let _this = this;
    _this.setData({
      showPopFP: true
    });
  },
  addMaintenance() {
    let _this = this;
    wx.redirectTo({
      url: "/pages/stockout/edit?maintenanceId=" + _this.data.modelid
    });
  },
  onClose() {
    let _this = this;
    _this.setData({
      showPop: false,
      showPopZP: false,
      showPopSL: false,
      showPopFP: false,
      showPopWG: false,
      showPJ: false,
      showPopZT: false,
    });
  },
  onClosePJ() {
    let _this = this;
    _this.setData({
      showPJ: false,
    });
  },
  goZP(e) {
    let _this = this;
    const {
      modelid,
      responsibleDepartmentId,
      contentAssign,
    } = _this.data;
    if (responsibleDepartmentId === 0) {
      app.showNotify('请选择维修部门');
      return;
    }
    app.ajax({
      url: 'Maintenance/Assign',
      again: false, // 重复调用
      data: {
        maintenanceId: modelid,
        responsibleDepartmentId,
        content: e.detail.value.Remark,
      },
      success: function (resp) {
        if (!resp.result) {
          app.showNotify(resp.msg)
        } else {
          wx.request({
            url: `${app.apiRoot}/Maintenance/SubscribeMessage`,
            method: 'POST',
            data: {
              id: _this.data.modelid,
              templateId: 'nQpdDl04GLhjY7EwrF3rk6orDUEI_WcDQ3lp5FUg09k'
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success(msgRes) {
              console.log(msgRes.data);
            }
          })
          wx.showToast({
            title: '指派成功',
            complete() {
              wx.navigateBack({
                delta: 1
              });
            }
          });
        }
      }
    });
  },
  goSL(e) {
    let _this = this;
    const {
      modelid,
      responsibleTeamId,
      maintenancePersons,
    } = _this.data;
    if (responsibleTeamId === 0) {
      app.showNotify('请选择维修班组');
      return;
    }
    const maintenancePersonIds = maintenancePersons.map(item => item.Id);
    app.ajax({
      url: 'Maintenance/Confirm',
      again: false, // 重复调用
      data: {
        maintenanceId: modelid,
        responsibleTeamId,
        maintenancePersonIds,
        pass: true
      },
      success: function (resp) {
        if (!resp.result) {
          app.showNotify(resp.msg)
        } else {
          wx.request({
            url: `${app.apiRoot}/Maintenance/SubscribeMessage`,
            method: 'POST',
            data: {
              id: _this.data.modelid,
              templateId: 'nQpdDl04GLhjY7EwrF3rk6orDUEI_WcDQ3lp5FUg09k'
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success(msgRes) {
              console.log(msgRes.data);
            }
          })
          wx.showToast({
            title: '确认维修成功',
            complete() {
              wx.navigateBack({
                delta: 1
              });
            }
          });
        }
      }
    });
  },
  toZP() {
    let _this = this;
    const {
      modelid,
    } = _this.data;
    wx.showModal({
      title: '申请指派',
      content: '确定申请重新指派吗？',
      complete: (res) => {
        if (res.cancel) {

        }

        if (res.confirm) {
          app.ajax({
            url: 'Maintenance/Confirm',
            again: false, // 重复调用
            data: {
              maintenanceId: modelid,
              pass: false
            },
            success: function (resp) {
              if (!resp.result) {
                app.showNotify(resp.msg)
              } else {
                wx.request({
                  url: `${app.apiRoot}/Maintenance/SubscribeMessage`,
                  method: 'POST',
                  data: {
                    id: _this.data.modelid,
                    templateId: 'nQpdDl04GLhjY7EwrF3rk6orDUEI_WcDQ3lp5FUg09k'
                  },
                  header: {
                    'content-type': 'application/x-www-form-urlencoded'
                  },
                  success(msgRes) {
                    console.log(msgRes.data);
                  }
                })
                wx.showToast({
                  title: '申请指派成功',
                  complete() {
                    wx.navigateBack({
                      delta: 1
                    });
                  }
                });
              }
            }
          });
        }
      }
    })
  },
  goFP(e) {
    let _this = this;
    const {
      modelid,
      responsibleTeamId,
      maintenancePersons,
    } = _this.data;
    if (responsibleTeamId === 0) {
      app.showNotify('请选择维修班组');
      return;
    }
    const maintenancePersonIds = maintenancePersons.map(item => item.Id);
    app.ajax({
      url: 'Maintenance/Allocate',
      again: false, // 重复调用
      data: {
        maintenanceId: modelid,
        responsibleTeamId,
        maintenancePersonIds,
      },
      success: function (resp) {
        if (!resp.result) {
          app.showNotify(resp.msg)
        } else {
          wx.request({
            url: `${app.apiRoot}/Maintenance/SubscribeMessage`,
            method: 'POST',
            data: {
              id: _this.data.modelid,
              templateId: 'nQpdDl04GLhjY7EwrF3rk6orDUEI_WcDQ3lp5FUg09k'
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success(msgRes) {
              console.log(msgRes.data);
            }
          });
          wx.showToast({
            title: '确认维修成功',
            complete() {
              wx.navigateBack({
                delta: 1
              });
            }
          });
        }
      }
    });
  },
  toPause() {
    let _this = this;
    const {
      modelid,
      model,
    } = _this.data;
    if (model.IsPause) {
      wx.showModal({
        title: '开始',
        content: '确定' + (model.IsPause ? '开始' : '暂停') + '吗？',
        complete: (res) => {
          if (res.cancel) {

          }

          if (res.confirm) {
            app.ajax({
              url: 'Maintenance/Pause',
              again: false, // 重复调用
              data: {
                maintenanceId: modelid,
                isStart: model.IsPause
              },
              success: function (resp) {
                if (!resp.result) {
                  app.showNotify(resp.msg)
                } else {
                  wx.showToast({
                    title: (model.IsPause ? '开始' : '暂停') + '成功',
                    complete() {
                      model.IsPause = !model.IsPause;
                      _this.setData({
                        model
                      });
                    }
                  });
                }
              }
            });
          }
        }
      })
    } else {
      _this.setData({
        showPopZT: true
      });
    }
  },
  goPause(e) {
    let _this = this;
    const {
      model,
    } = _this.data;
    if (!e.detail.value.Remark) {
      app.showNotify('请输入暂停说明');
      return;
    }
    app.ajax({
      url: 'Maintenance/Pause',
      again: false, // 重复调用
      data: {
        maintenanceId: model.Id,
        content: e.detail.value.Remark,
        isStart: model.IsPause
      },
      success: function (resp) {
        if (!resp.result) {
          app.showNotify(resp.msg)
        } else {
          wx.showToast({
            title: (model.IsPause ? '开始' : '暂停') + '成功',
            complete() {
              model.IsPause = !model.IsPause;
              _this.setData({
                model,
                showPopZT: false,
              });
            }
          });
        }
      },
      fail(resp) {
        console.log('fail', resp);
      }
    });
  },
  doSave(e) {
    let _this = this;
    let _imgs = '';
    let _picDone = true;
    for (let i = 0; i < _this.data.files.length; i++) {
      if (_this.data.files[i].id <= 0) {
        _picDone = false;
        break;
      } else {
        _imgs += _this.data.files[i].id + ',';
      }
    }
    if (!_picDone) {
      app.showNotify('请等待图片上传完成');
      return;
    }
    if (!e.detail.value.Remark) {
      app.showNotify('请输入评价内容');
      return;
    }
    app.ajax({
      url: 'Maintenance/Comment',
      again: false, // 重复调用
      data: {
        content: e.detail.value.Remark,
        star: e.detail.value.Star,
        maintenanceId: _this.data.modelid,
        typeId: _this.data.type,
        SelectedPictureIds: _imgs
      },
      success: function (resp) {
        if (!resp.result) {
          app.showNotify(resp.msg)
        } else {
          wx.showToast({
            title: '提交成功',
            complete() {
              _this.setData({
                showPop: false
              });
              _this.loadDetail(_this.data.modelid);
            }
          });
        }
      }
    });
  },
  doSaveWG(e) {
    let _this = this;
    const {
      modelid,
      model,
      details,
    } = _this.data;
    let _imgs = '';
    let _picDone = true;
    for (let i = 0; i < _this.data.files.length; i++) {
      if (_this.data.files[i].id <= 0) {
        _picDone = false;
        break;
      } else {
        _imgs += _this.data.files[i].id + ',';
      }
    }
    if (!_picDone) {
      app.showNotify('请等待图片上传完成');
      return;
    }
    if (_imgs.length === 0) {
      app.showNotify('请上传现场图片');
      return;
    }
    app.ajax({
      url: 'Maintenance/Complete',
      again: false, // 重复调用
      data: {
        maintenanceId: modelid,
        content: e.detail.value.Remark,
        stockId: model.EquipmentStockId,
        isExternal: model.IsExternal,
        total: e.detail.value.Total,
        SelectedPictureIds: _imgs,
        details,
      },
      success: function (resp) {
        if (!resp.result) {
          app.showNotify(resp.msg)
        } else {
          wx.request({
            url: `${app.apiRoot}/Maintenance/SubscribeMessage`,
            method: 'POST',
            data: {
              id: _this.data.modelid,
              templateId: 'nQpdDl04GLhjY7EwrF3rk6orDUEI_WcDQ3lp5FUg09k'
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success(msgRes) {
              console.log(msgRes.data);
            }
          })
          wx.showToast({
            title: '提交成功',
            complete() {
              _this.setData({
                showPopWG: false
              });
              _this.loadDetail(_this.data.modelid);
            }
          });
        }
      }
    });
  },
  startCamera() {
    const {
      modelid
    } = this.data;
    wx.navigateTo({
      url: '/pages/camera/index?id=' + modelid,
    });
  },
  playCamera() {
    const _this = this;
    _this.setData({
      videoUrl: _this.data.model.Videos[0],
      showVideo: true
    });
    _this.videoContext.play();
  },
  playPersonCamera(e) {
    let _this = this;
    const {
      videos
    } = e.currentTarget.dataset;
    _this.setData({
      videoUrl: videos[0],
      showVideo: true
    });
    _this.videoContext.play();
  },
  //移除已有录像
  removeCamera() {
    let _this = this;
    _this.setData({
      hasCamera: false
    });
  },
  hideVideo() {
    this.videoContext.stop();
    this.setData({
      showVideo: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  showDepart(e) {
    this.setData({
      showDepart: true,
    });
  },

  showTeam(e) {
    this.setData({
      showTeam: true,
    });
  },

  showPerson(e) {
    this.setData({
      showPerson: true,
    });
  },

  closeDepart() {
    this.setData({
      showDepart: false
    });
  },

  closeTeam() {
    this.setData({
      showTeam: false
    });
  },

  closePerson() {
    this.setData({
      showPerson: false
    });
  },

  confirmDepart(e) {
    const _this = this;
    const {
      detail: {
        index,
        value: {
          id,
          name
        }
      }
    } = e;
    this.setData({
      showDepart: false,
      departIndex: index,
      departName: name,
      responsibleDepartmentId: id,
    });
  },

  confirmTeam(e) {
    const _this = this;
    const {
      model
    } = _this.data;
    const {
      detail: {
        index,
        value: {
          id,
          name
        }
      }
    } = e;
    const _persons = (model.AvailableUsers || []).filter(m => m.PId === id).map(s => ({
      Id: s.Id,
      Text: s.Text,
      Checked: false,
    }));
    this.setData({
      showTeam: false,
      teamIndex: index,
      teamName: name,
      responsibleTeamId: id,
      maintenancePersons: [],
      maintenancePersonNames: '',
      persons: _persons,
    });
  },

  onCheckPersonChange(e) {
    const _this = this;
    const {
      maintenancePersons,
      persons
    } = _this.data;
    let _maintenancePersons = maintenancePersons;
    const {
      currentTarget: {
        dataset: {
          id
        }
      },
      detail
    } = e;

    const person = persons.filter(m => m.Id === id)[0];
    if (person) {
      if (detail) {
        const thisStock = maintenancePersons.filter(m => m.Id === person.Id)[0];
        if (!thisStock) {
          _maintenancePersons.push(person);
        }
      } else {
        _maintenancePersons = _maintenancePersons.filter(m => m.Id != person.Id);
      }
      person.Checked = detail;
    }
    _this.setData({
      maintenancePersons: _maintenancePersons,
      maintenancePersonNames: _maintenancePersons.map(item => item.Text).join(','),
      persons
    });
  },

  // 输入框值变化
  onRemarkChange(e) {
    const _this = this;
    const {
      detail
    } = e;
    _this.setData({
      remark: detail
    });
  },

  // 输入框值变化
  onStepperChange(e) {
    const _this = this;
    const {
      remark
    } = _this.data;
    const {
      detail
    } = e;
    _this.setData({
      star: detail,
      remark: detail < 90 ? '' : remark
    });
  },
  loadEquipments(push = false) {
    let _this = this;
    const {
      filter: {
        categoryId,
        placeId,
        name,
        pp,
        gg,
        location,
      },
      pageIndex,
      totalPages,
      pageSize,
      items,
      details,
    } = _this.data;
    if (push && pageIndex + 1 >= totalPages) return;
    let ids = details.map(m => m.EquipmentStockId);
    app.ajax({
      url: 'Stock/List',
      data: {
        categoryId,
        placeId,
        name,
        pp,
        gg,
        location,
        pageIndex: push ? pageIndex + 1 : pageIndex,
        pageSize
      },
      success(resp) {
        if (resp.result && resp.model) {
          let _lists = (resp.model || []).map(m => {
            m.Checked = ids.indexOf(m.Id) !== -1;
            return m;
          });
          const list = push ? [...items, ..._lists] : _lists;
          _this.setData({
            items: list,
            pageIndex: resp.PageIndex,
            totalPages: resp.TotalPages,
          });
        }
      }
    });
  },
  scrolltolower() {
    this.loadEquipments(true);
  },
  showPopupPJ() {
    this.loadEquipments();
    this.setData({
      showPJ: true
    });
  },
  onPlaceSearch({
    detail
  }) {
    const {
      allPlaces
    } = this.data;
    const _places = (allPlaces || []).filter(m => m.Text.indexOf(detail) != -1);
    this.setData({
      searchPlaceValue: detail,
      places: _places,
    });
  },
  onPlaceFocus() {
    this.setData({
      showPlaceAction: true
    });
  },
  onPlaceCancel() {
    const {
      allPlaces
    } = this.data;
    this.setData({
      showPlaceAction: false,
      searchPlaceValue: '',
      places: allPlaces,
    });
  },
  selectPlace(e) {
    const {
      target: {
        id
      }
    } = e;
    let _this = this;
    const {
      filter,
      allPlaces
    } = _this.data;
    const place = allPlaces.filter(m => m.Id == id * 1)[0];
    if (place) {
      filter.placeId = place.Id;
      filter.PlaceName = place.Text;
      _this.setData({
        showPlace: false,
        filter
      });
    }
  },

  onCheckChange(e) {
    const _this = this;
    const {
      modelid,
      details,
      items
    } = _this.data;
    let _details = details;
    const {
      currentTarget: {
        dataset: {
          id
        }
      },
      detail
    } = e;

    const stock = items.filter(m => m.Id === id)[0];
    if (stock) {
      if (detail) {
        const thisStock = details.filter(m => m.EquipmentStockId === stock.Id)[0];
        if (!thisStock) {
          _details.push({
            MaintenanceId: modelid,
            EquipmentStockId: stock.Id,
            EquipmentId: stock.EquipmentId,
            Name: stock.Name,
            Manufacturer: stock.Manufacturer,
            Specification: stock.Specification,
            Location: stock.Location,
          })
        }
      } else {
        _details = details.filter(m => m.EquipmentStockId != stock.Id);
      }
      stock.Checked = detail;
    }
    _this.setData({
      details: _details,
      items
    });
  },

  onPJDelete(e) {
    const _this = this;
    const {
      details,
    } = _this.data;
    let _details = details;
    const {
      currentTarget: {
        dataset: {
          sid,
        }
      }
    } = e;
    _details = details.filter(m => m.EquipmentStockId != sid);
    _this.setData({
      details: _details
    });
  },
  // 根据筛选条件查询
  onSearch() {
    const _this = this;
    _this.selectComponent('#item').toggle();
    _this.setData({
      pageIndex: 0,
      totalPages: 1
    });
    _this.loadEquipments();
  },
  // 根据筛选条件查询
  onCancel() {
    const _this = this;
    const {
      filter
    } = _this.data;
    _this.selectComponent('#item').toggle();
    filter.categoryId = 0;
    filter.placeId = 0;
    filter.CategoryName = '';
    filter.PlaceName = '';
    filter.name = '';
    filter.pp = '';
    filter.gg = '';
    _this.setData({
      filter,
      pageIndex: 0,
      totalPages: 1
    });
    _this.loadEquipments();
  },

  // 输入框值变化
  onFieldChange(e) {
    const _this = this;
    const {
      filter
    } = _this.data;
    const {
      detail,
      target: {
        id
      }
    } = e;
    filter[id] = detail;
    this.setData({
      filter
    });
  },

  // 输入框值变化
  onSwitchChange(e) {
    const _this = this;
    const {
      filter
    } = _this.data;
    const {
      detail,
      target: {
        id
      }
    } = e;
    filter[id] = detail;
    this.setData({
      filter
    });
  },

  // 显示设备分类选择框
  showCategory(e) {
    this.setData({
      showCategory: true
    });
  },

  // 显示巡检点选择框
  showPlace(e) {
    this.setData({
      showPlace: true,
    });
  },

  closeCategory() {
    this.setData({
      showCategory: false
    });
  },

  closePlace() {
    this.setData({
      showPlace: false
    });
  },

  onCategoryClickNav({
    detail = {}
  }) {
    this.setData({
      categoryActiveIndex: detail.index || 0
    });
  },

  onCategoryClickItem({
    detail = {}
  }) {
    const {
      filter,
    } = this.data;
    const categoryActiveId = detail.id;
    filter.categoryId = categoryActiveId;
    filter.CategoryName = detail.text;
    this.setData({
      categoryActiveId,
      filter,
      showCategory: false,
    });
  },

  // 输入框值变化
  onIsExternalChange(e) {
    const _this = this;
    const {
      detail
    } = e;
    this.setData({
      isExternal: detail
    });
  },
})